package com.szeastroc.service.system.impl;

import com.szeastroc.base.service.BaseServiceImpl;
import com.szeastroc.bean.system.SystemResource;
import com.szeastroc.bean.system.SystemRoleResource;
import com.szeastroc.bean.system.ZTreeNode;
import com.szeastroc.mapper.system.SystemResourceMapper;
import com.szeastroc.service.system.SystemResourceService;
import com.szeastroc.service.system.SystemRoleResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by jie on 2018/1/6.
 */
@Service
public class SystemResourceImpl extends BaseServiceImpl<SystemResource> implements SystemResourceService {

    @Autowired
    private SystemResourceMapper systemResourceMapper;

    @Autowired
    private SystemRoleResourceService systemRoleResourceService;

    @Override
    public List<SystemResource> getSystemResource(Integer userId,Integer roleId) {
        List<SystemResource> list = systemResourceMapper.getParentResourceByUserId(userId);
        for (SystemResource systemResource : list) {
            List<SystemResource> srs = systemResourceMapper.getResourceByParentIdAndUserId(systemResource.getId(),roleId);
            systemResource.setSysytemResources(srs);
        }
        return list;
    }

    public List<SystemResource> getSystemResourceByRoleId(Integer roleId){
        return systemResourceMapper.getSystemResourceByRoleId(roleId);
    }


    //根据角色id查询出权限树（并勾选上角色拥有的权限）
    public List<ZTreeNode> getAllSystemResourceTreeByRoleId(Integer roleId) {
        List<ZTreeNode> list = new LinkedList<ZTreeNode>();
        List<SystemResource> parentResource = systemResourceMapper.getResourceByParentId(0);
        List<Integer> roleIds = systemResourceMapper.getResourceIdByRoleId(roleId);

        // 设置 父级(大类),二级(功能),三级菜单(按钮)
        for (SystemResource systemResource : parentResource) {
            //父级
            ZTreeNode zParent = new ZTreeNode();
            zParent.setId(systemResource.getId());
            zParent.setName(systemResource.getName());
            zParent.setIcon(systemResource.getIcon());
            if (roleIds.contains(systemResource.getId())) {
                zParent.setChecked(true);
            }

            //二级
            List<ZTreeNode> secondlist = new LinkedList<ZTreeNode>();
            List<SystemResource> secondLevel = systemResourceMapper.getResourceByParentId(systemResource.getId());
            for (SystemResource resource2 : secondLevel) {
                ZTreeNode secondParent = new ZTreeNode();
                secondParent.setId(resource2.getId());
                secondParent.setName(resource2.getName());
                secondParent.setIcon(resource2.getIcon());
                if (roleIds.contains(secondParent.getId())) {
                    secondParent.setChecked(true);
                }

                //三级按钮
                List<ZTreeNode> thirdlist = new LinkedList<ZTreeNode>();
                List<SystemResource> thirdLevel = systemResourceMapper.getResourceByParentId(resource2.getId());
                for (SystemResource resource3 : thirdLevel) {
                    ZTreeNode thirdParent = new ZTreeNode();
                    thirdParent.setId(resource3.getId());
                    thirdParent.setName(resource3.getName());
                    thirdParent.setIcon(resource3.getIcon());
                    if (roleIds.contains(thirdParent.getId())) {
                        thirdParent.setChecked(true);
                    }

                }
                secondParent.setChildren(thirdlist);

                //添加二级菜单
                secondlist.add(secondParent);
            }
            zParent.setChildren(secondlist);
            list.add(zParent);
        }

        return list;
    }

    @Transactional
    public void saveRoleResources(String resourceIds, Integer roleId) {
        systemRoleResourceService.deleteByRoleId(roleId);

        if (StringUtils.isBlank(resourceIds)) {
            return;
        }
        List<SystemRoleResource> resources = new ArrayList<SystemRoleResource>();
        String[] ids = resourceIds.split(",");
        for (String id : ids) {
            resources.add(new SystemRoleResource(roleId, Integer.parseInt(id)));
        }
        systemRoleResourceService.insertList(resources);

    }

    @Transactional
    public void saveResourcesStatus(Integer resourceId,Integer status){
        systemResourceMapper.saveResourcesStatus(resourceId, status);
    }

    public List<SystemResource> getSystemResourceParentByRoleIds(String ids){
        return systemResourceMapper.getSystemResourceParentByRoleIds(ids);
    }

    public List<SystemResource> getSystemResourceByParentId(Integer id){
        return systemResourceMapper.getSystemResourceByParentId(id);
    }
}
